// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-18 10:23
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DateTrain;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

/*
    对给定范围的数字的字典序进行排序
    // 从小到大遍历 -> 简单模拟
 */
public class LexicalOrder {
    public List<Integer> lexicalOrder(int n) {
        List<Integer> ans = new ArrayList<>();
        for (int i = 1, k = 1; i <= n; i++) {
            ans.add(k);
            if (k * 10 < n) {
                k *= 10;
            } else {
                while (k % 10 == 9 || k + 1 > n) {
                    k /= 10;
                }
                k++;
            }
        }
        return ans;
        //return IntStream.rangeClosed(1, n).mapToObj(i -> i + "").sorted().mapToInt(Integer::parseInt).boxed().collect(toList());
    }
}
